//
//  PictureController.m
//  Happy to chat
//
//  Created by lanou3g on 15/12/8.
//  Copyright © 2015年 LL. All rights reserved.
//

#import "PictureController.h"
#import "PictureViewCell.h"
#import "PictureModel.h"
#import "MJRefresh.h"

@interface PictureController ()

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation PictureController

int page = 1;
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView.estimatedRowHeight = 44;
    //上拉刷新和下拉加载
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self parseFoot];
    }];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self parse];
    }];
    
    self.navigationController.hidesBarsOnSwipe = YES;
    
    [self parse];
}
#pragma mark -- 网络解析数据

- (void) parseFoot{
    //解析图片
    PictureModel *model = [self.dataArray lastObject];
    NSString *string = [NSString stringWithFormat:kNextUrl,page++,model.update_time];
    
    NSMutableURLRequest *quest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:string]];
    
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionTask *task = [session dataTaskWithRequest:quest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (data) {
            
            NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            
            NSArray *array = dictionary[@"items"];
            
            for (NSDictionary *dic in array) {
                
                PictureModel *model = [PictureModel initWithDic:dic];
                
                [self.dataArray addObject:model];
                
            }
            
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
            
            
            
        });
        
    }];
    
    [task resume];
}
- (void)parse{
    
    page = 1;
    if (self.dataArray) {
        
        [self.dataArray removeAllObjects];
    }
    
    NSMutableURLRequest *quest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:kFirstUrl]];
    
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionTask *task = [session dataTaskWithRequest:quest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (data) {
            
            NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            
            NSArray *array = dictionary[@"items"];
            
            for (NSDictionary *dic in array) {
                
                PictureModel *model = [PictureModel initWithDic:dic];
                
                [self.dataArray addObject:model];
                
            }
            
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            
        });
        
    }];
    
    [task resume];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PictureViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    PictureModel *model = self.dataArray[indexPath.row];
    
    cell.model = model;
    
    return cell;
}

#pragma mark --懒加载
- (NSArray *)dataArray{
    
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
@end
